package org.example.easybuy.controller;

import com.antherd.smcrypto.sm3.Sm3;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.example.easybuy.entity.User;
import org.example.easybuy.service.UserService;
import org.example.easybuy.util.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    ObjectMapper objectMapper=new ObjectMapper();

    @GetMapping("/getUserPage")
    public CustomResult getPageInfo(@RequestParam Map<String,Object> params)
    {
        if(!StringUtils.isNullOrEmpty(params.get("userName").toString()))
        {
            params.put("userName","%"+params.get("userName")+"%");
        }
        if(!StringUtils.isNullOrEmpty(params.get("createTime").toString()))
        {
            params.put("createTime","%"+params.get("createTime")+"%");
        }
        PageInfo<User> pageInfo=userService.getUserPageInfo(params);
        String userStr=stringRedisTemplate.opsForValue().get("user");
        User user=null;
        try {
            user=objectMapper.readValue(userStr,User.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return CustomResult.successResult().addResult("pageInfo",pageInfo).addResult("user",user);
    }

    @RequestMapping("/deleteUser")
    public CustomResult deleteUser(int id)
    {
        if(userService.deleteUser(id))
        {
            return CustomResult.successResult().addResult("isDelete","yes");
        }
        else{
            return CustomResult.successResult().addResult("isDelete","no");
        }
    }

    @PostMapping("/updateUser")
    public CustomResult updateUser(@RequestBody User user){
        System.out.println("==========="+user);
        if(userService.updateUser(user)){
            return CustomResult.successResult().addResult("isUpdate","yes");
        }
        return CustomResult.successResult().addResult("isUpdate","no");
    }

    @GetMapping("/updateUserPass")
    public CustomResult updateUserPass(String pass,int id)
    {
        String newPass= Sm3.sm3(pass);
        if(userService.updateUserPass(id,newPass))
        {
            return CustomResult.successResult().addResult("isUpdate","yes");
        }
        return CustomResult.successResult().addResult("isUpdate","no");
    }

    @GetMapping("/getUserByLoginName")
    public CustomResult getUserByLoginName(String loginName)
    {
        return CustomResult.successResult() .addResult("user",userService.getUserByLoginName(loginName));
    }

    @PostMapping("/updateUserInfo")
    public CustomResult updateUserInfo(@RequestBody User user){
        String password=user.getPassword();
        if(!password.equals(""))
        {
            user.setPassword(Sm3.sm3(password));
        }
        if(userService.updateUserInfo(user))
        {
            return CustomResult.successResult().addResult("isUpdate","yes");
        }
        return CustomResult.errorResult(666,"修改失败").addResult("isUpdate","no");
    }

    @GetMapping("/modifyUserPassword")
    public CustomResult modifyUserPassword(String oldPassword,String password)
    {
        if(Sm3.sm3(oldPassword).equals(password))
        {
            //原密码输入正确
            return CustomResult.successResult().addResult("pass","yes");
        }
        return CustomResult.successResult().addResult("pass","no");
    }
}
